package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.recyclertype.Article;
import com.yidiandao.business.data.recyclertype.TopArticle;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.NumberFormatUtils;
import com.yidiandao.business.utils.UIUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 新闻page的顶部provider
 * <p>
 * Created by SunDing(dindding@163.com) on 2017/4/12 14:59
 */
public class DiscoveryNewsTopPageProvider extends
        ItemViewProvider<TopArticle, DiscoveryNewsTopPageProvider.ViewHolder>{
    private Activity mActivity;

    public DiscoveryNewsTopPageProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View rootView = inflater.inflate(R.layout.item_discovery_news_top, parent, false);
        return new ViewHolder(rootView);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder viewHolder, @NonNull final TopArticle topArticle) {
        Article bean = topArticle.getArticle();
        //图片
        ImageLoader.loadBanner(mActivity, viewHolder.mIvPic, bean.getArticleThumb());
        //标题
        viewHolder.mTvTitle.setText(bean.getArticleTitle());
        //阅读数
        viewHolder.mTvRead.setText(UIUtils.getString(R.string.discovery_has_read, NumberFormatUtils.getLargeFormatValue(bean.getWatchCount())));
        //文章id
        final String articleId = bean.getArticleId();
        viewHolder.mRootItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UrlActivity.actionStartForArticle(mActivity, articleId);
            }
        });

    }
    static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mIvPic;
        private TextView mTvTitle;
        private TextView mTvRead;
        private RelativeLayout mRootItem;

        private ViewHolder(View view) {
            super(view);
            mRootItem = (RelativeLayout) view.findViewById(R.id.root_item);
            mIvPic = (ImageView) view.findViewById(R.id.iv_pic);
            mTvTitle = (TextView) view.findViewById(R.id.tv_title);
            mTvRead = (TextView) view.findViewById(R.id.tv_read);

        }
    }
}
